package cheme.ProcessEditor.Parts.Equations;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.gef.EditPolicy;

import cheme.ProcessEditor.Figures.FieldFig;
import cheme.ProcessEditor.Parts.PropertyAwarePart;
import cheme.ProcessEditor.Policies.FieldPolicy;
import cheme.ProcessModel.Equations.EquationPaletteField;

public class PartEquationPaletteField extends PropertyAwarePart
{
	@Override
	protected FieldFig createFigure()
	{
		FieldFig fig = new FieldFig(getModel().getText());
		
		fig.setToolTip(new Label("An operation in an equation.\n(Drag and drop in the desried part of an equation)"));
		
		initFigurePresentation(fig);
		
		return fig;
	}
	
	protected void initFigurePresentation(FieldFig figure)
	{
		if(modelHasTooltipText())
		{
			figure.setToolTip(getToolTipFigure());
		}
		
		figure.setInsets(new Insets(2,5,3,5));
		
//		figure.setBackgroundColor(ChemePresentation.Fields.Equations.Palette.background);
//		figure.setForegroundColor(ChemePresentation.Fields.Equations.Palette.foreground);
	}
	
	protected IFigure getToolTipFigure()
	{
		IFigure toolTip = null;
		
		if(modelHasTooltipText())
		{
			toolTip = new Label(getModel().getToolTipText());
		}
		
		return toolTip;
	}
	
	private boolean modelHasTooltipText()
	{
		return !(getModel().getToolTipText().isEmpty()); 
	}
	
	@Override
	protected void createEditPolicies()
	{
		installEditPolicy(EditPolicy.COMPONENT_ROLE, new FieldPolicy());
		installEditPolicy(EditPolicy.LAYOUT_ROLE, null);
	}

	public EquationPaletteField getModel()
	{
		return (EquationPaletteField)super.getModel();
	}
	
	public FieldFig getFigure()
	{
		return (FieldFig)super.getFigure();
	}
}
